<?php
/**
 * Item Model
 *
 * @property Item
 */
class Country extends AppModel {
/**
 * Display field
 *
 * @var string
 */

    /**
     * associations
     *
     * @var array
     **/

    var $hasMany = array(
        'CountryOperator' => array(
            'className' => 'CountryOperator',
            'foreignKey' => 'country_id',
            'dependent' => false,
            'conditions' => array('CountryOperator.deleted'=>'n'),
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );


    function getCountryById($id = null){
        $conditions = array(
            'Country.id' => $id,
            'Country.deleted' => 'n'
        );
        # fetch the specific data from the server and retrun it.
        return  $this->find('first', array('conditions' => $conditions, 'recursive' => -1));
    }


    function getCountryAndOperator(){
        # fetch the specific data from the server and retrun it.
        return  $this->find('all', array('conditions'=>array('Country.deleted' => 'n'),'contain' => array('CountryOperator'), 'recursive' => -1));
    }

    function getCountryOptions(){
        # fetch the specific data from the server and retrun it.
        $data = $this->find('all', array('conditions'=>array('Country.deleted' => 'n'), 'recursive' => -1));
        $res = array();
        if($data){
            foreach($data as $d){
                $res[] = array(
                    'id'=>$d['Country']['id'],
                    'name'=>$d['Country']['name']
                );
            }
        }
        return  $res;
    }

    function getCountryAndOperatorForWidget(){
        $data = $this->getCountryAndOperator();
        $res = array();
        if($data){
            foreach($data as $d){
                $operators = $d['CountryOperator'];
                $opr_arr = array();
                foreach($operators as $op){
                    $opr_arr[] = $op['operator'];
                }

                $res[] = array(
                    'country'=>$d['Country']['name'],
                    'operators'=>implode(',  ',$opr_arr)
                );
            }
        }
        return  $res;
    }





}
